package com.witframework.baseapp.security.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.witframework.baseapp.security.DataSetAuth;
import com.witframework.baseapp.security.DataSetAuthVo;
import com.witframework.baseapp.security.service.DataSetAuthService;
import com.witframework.core.WitFrameWork;
import com.witframework.core.logic.BaseService;

@Service("dataSetAuthService")
public class DataSetAuthServiceImpl extends BaseService implements DataSetAuthService{

	public List<DataSetAuthVo> getAllAuth(Map filter,String category) {	
		List<DataSetAuthVo> vos=new ArrayList<DataSetAuthVo>();
		filter.put("isactive", "Y");
		filter.put("isdelete", "N");	
		filter.put("category", category);
		List<DataSetAuth> all=WitFrameWork.getSimpleBaseService(DataSetAuth.class).findBy(filter);	
		for(DataSetAuth dataSetAuth:all){			
			setAuth(vos,dataSetAuth);
		}			
		return vos;
	}
	private void setAuth(List<DataSetAuthVo> vos,DataSetAuth dataSetAuth){
		for(DataSetAuthVo vo:vos){	
			if(StringUtils.isEmpty(dataSetAuth.getTarget())){
				if(vo.getUsername().equals(dataSetAuth.getUsername())){
					vo.addAuthname(dataSetAuth.getName());
					return;
				}
			}else{
				if(vo.getUsername().equals(dataSetAuth.getUsername())&&dataSetAuth.getTarget().equals(vo.getTarget())){
					vo.addAuthname(dataSetAuth.getName());
					return;
				}
			}
		}
		DataSetAuthVo vo=new DataSetAuthVo();
		vo.setId(dataSetAuth.getId());
		vo.setCategory(dataSetAuth.getCategory());
		vo.setUsername(dataSetAuth.getUsername());
		vo.setUsercnname(dataSetAuth.getUsercnname());
		vo.setTarget(dataSetAuth.getTarget());
		vo.setTargetname(dataSetAuth.getTargetname());
		vo.setRole_id(dataSetAuth.getRoleId());	
		vo.addAuthname(dataSetAuth.getName());		
		vos.add(vo);
	}

	public List<DataSetAuthVo> getUserDataSetAuth(String username) {
		List<DataSetAuthVo> vos=new ArrayList<DataSetAuthVo>();
		List<DataSetAuth> all=WitFrameWork.getSimpleBaseService(DataSetAuth.class).findBy("username",username);
		for(DataSetAuth dataSetAuth:all){			
			setAuth(vos,dataSetAuth);
		}	
		return vos;
	}

	public List<DataSetAuthVo> getUserDataSetAuth(String username, String category) {
		List<DataSetAuthVo> vos=new ArrayList<DataSetAuthVo>();
		Map filter=new HashMap();
		filter.put("username",username);
		filter.put("category",category);
		List<DataSetAuth> all=WitFrameWork.getSimpleBaseService(DataSetAuth.class).findBy(filter);
		for(DataSetAuth dataSetAuth:all){			
			setAuth(vos,dataSetAuth);
		}
		return vos;
	}
	public DataSetAuthVo getUserDataSetAuth(String username, String category,String target) {
		List<DataSetAuthVo> vos=new ArrayList<DataSetAuthVo>();
		Map filter=new HashMap();
		filter.put("username",username);
		filter.put("category",category);
		filter.put("target",target);
		List<DataSetAuth> all=WitFrameWork.getSimpleBaseService(DataSetAuth.class).findBy(filter);
		for(DataSetAuth dataSetAuth:all){			
			setAuth(vos,dataSetAuth);
		}
		return vos.get(0);
	}
	
}
